package com.foot.travel.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.foot.travel.R;
import com.foot.travel.event.NetRefreshEvent;
import com.koalac.ironman.common.module.ModuleActivity;
import com.koalac.ironman.net.MyRequest;
import com.koalac.ironman.net.ResponseCallback;
import com.koalac.ironman.net.pojo.request.GetOrder;
import com.koalac.ironman.net.pojo.request.SubmitOrder;
import com.koalac.ironman.net.pojo.response.OrderDetail;
import com.koalac.ironman.net.pojo.response.PayResultEvent;
import com.koalac.ironman.net.pojo.response.Response;
import com.koalac.ironman.util.BeeCloudUtil;
import com.koalac.ironman.viewhelper.ViewInject;
import com.squareup.otto.Subscribe;

public class OrderDetailActivity extends ModuleActivity implements
	OnClickListener {

    @ViewInject(id = R.id.route)
    TextView mRouteView;
    @ViewInject(id = R.id.date)
    TextView mDateView;
    @ViewInject(id = R.id.number)
    TextView mNumberView;

    @ViewInject(id = R.id.unit_price)
    TextView mUnitPriceView;
    @ViewInject(id = R.id.total_price)
    TextView mTotalPriceView;
    @ViewInject(id = R.id.down_payment)
    TextView mDownPaymentView;

    @ViewInject(id = R.id.school)
    TextView mSchoolView;
    @ViewInject(id = R.id.class_name)
    TextView mClassNameView;
    @ViewInject(id = R.id.linkman_name)
    TextView mLinkNameView;

    @ViewInject(id = R.id.linkman_phone)
    TextView mLinkPhoneView;
    @ViewInject(id = R.id.mail)
    TextView mMailView;
    @ViewInject(id = R.id.alipay)
    TextView mPayView;
    @ViewInject(id = R.id.pay_order_btn, click = "onClick")
    TextView mPayOrderView;
    @ViewInject(id = R.id.parent_view)
    View mParentView;
    @ViewInject(id = R.id.bottom_layout)
    View mBottomLayout;

    SubmitOrder mSubmitOrder;
    OrderDetail mOrderDetail;

    public final static String ORDER_ID = "order_id";
    private String mOrderId;
    private String mPayOrderId;
    private double mTotalPrice;
    BeeCloudUtil mBeeCloudUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setBackButtonVisibility(true);
	mParentView.setVisibility(View.GONE);
	setNavigationTitle("订单信息");
	mOrderId = getIntent().getStringExtra(ORDER_ID);
	getOrderDetail();
	mBeeCloudUtil = new BeeCloudUtil(OrderDetailActivity.this); 
    }


    private void getOrderDetail() {
	GetOrder getOrder = new GetOrder();
	getOrder.setId(mOrderId);
	MyRequest.request(OrderDetailActivity.this, getOrder,
		new ResponseCallback() {

		    @Override
		    public void process(boolean result, Context context,
			    Response data, String msg) {
			if (result) {
			    mParentView.setVisibility(View.VISIBLE);
			    mOrderDetail = data.getObject(OrderDetail.class);
			    mRouteView.setText(mOrderDetail.getProduct_name());
			    mDateView.setText(mOrderDetail.getStart_date());
			    mNumberView.setText(mOrderDetail.getNumber() + "人");

			    mUnitPriceView.setText(mOrderDetail.getPer_price()
				    + "元");
			    double perPrice = Double.parseDouble(mOrderDetail
				    .getPer_price());
			    mTotalPrice = perPrice
				    * Integer.parseInt(mOrderDetail.getNumber());

			    mTotalPriceView.setText(mTotalPrice + "元");
			    mDownPaymentView.setText(mOrderDetail
				    .getFront_money() + "元");

			    mSchoolView.setText(mOrderDetail.getSchool());
			    mClassNameView.setText(mOrderDetail.getClassName());
			    mLinkNameView.setText(mOrderDetail
				    .getContact_name());

			    mLinkPhoneView.setText(mOrderDetail
				    .getContact_phone());
			    // mPayView.setText(mOrderDetail.getPay_way());
			    mPayView.setText("支付宝");
			    initSubmitOrder();
			    if (mOrderDetail.getFlag().equals("1")) {
				mPayOrderView.setVisibility(View.GONE);
				mBottomLayout.setVisibility(View.GONE);
			    }
			    mPayOrderId = mOrderDetail.getBook_id();
			    setNetworkVisibility(false);
			}else {
			    setNetworkVisibility(true);
			}
		    }
		});
    }
    
    @Subscribe
    public void onRefresh(NetRefreshEvent netRefreshEvent) {
	setNetworkVisibility(false);
	getOrderDetail();
    }

    private void initSubmitOrder() {
	mSubmitOrder = new SubmitOrder();

	mSubmitOrder.setProductId(mOrderDetail.getProduct_id());
	mSubmitOrder.setProductName(mOrderDetail.getProduct_name());
	mSubmitOrder.setProductFlag(mOrderDetail.getProduct_flag());

	mSubmitOrder.setNumber(mOrderDetail.getNumber());
	mSubmitOrder.setStartDate(mOrderDetail.getStart_date());
	mSubmitOrder.setSchool(mOrderDetail.getSchool());

	mSubmitOrder.setClassName(mOrderDetail.getContact_name());
	mSubmitOrder.setContactName(mOrderDetail.getContact_name());
	mSubmitOrder.setContactPhone(mOrderDetail.getContact_phone());

	mSubmitOrder.setEmail(mOrderDetail.getEmail());
	mSubmitOrder.setRemark(mOrderDetail.getRemark());
	mSubmitOrder.setPerPrice(mOrderDetail.getPer_price());

	mSubmitOrder.setTotalPrice("" + mTotalPrice);
	mSubmitOrder.setFrontMoney(mOrderDetail.getFront_money());
	mSubmitOrder.setPayWay("支付宝");

    }

    @Override
    protected void initLayout(ViewGroup rootContentView) {
	addContentView(R.layout.activity_pay_order);
    }
    
    @Subscribe
    public void onReceivePayResult(PayResultEvent payResultEvent){
	if (payResultEvent.isPaySuccess()) {
	    Intent intent = new Intent(OrderDetailActivity.this,PayFinishActivity.class);
	    intent.putExtra(PayFinishActivity.ORDER_ID, mOrderId);
	    startActivity(intent);
	}
    }

    @Override
    public void onClick(View v) {
	int viewId = v.getId();
	switch (viewId) {
	case R.id.pay_order_btn:
	    double totalPrice = Double.parseDouble(mOrderDetail.getPer_price()) * Integer.parseInt(mOrderDetail.getNumber());
	    double perPrice = Double.parseDouble(mOrderDetail.getFront_money());
//	    new AliPayUtil().pay(OrderDetailActivity.this, mHandler,mOrderId,mOrderDetail.getProduct_name(),"总价:"+totalPrice+"元 支付定金:" + mOrderDetail.getFront_money()+"元");
	    mBeeCloudUtil.alipay(mOrderDetail.getProduct_name(), ((int)perPrice * 100), mPayOrderId);
	    break;
	default:
	    break;
	}
    }

}
